//
//  CollectionDetailFirstCell.m
//  Tang
//
//  Created by 邓芮 on 15/12/7.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "CollectionDetailFirstCell.h"

@interface CollectionDetailFirstCell ()
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UILabel *contents;
@end


@implementation CollectionDetailFirstCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = NO;
        self.icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWidth, 0)];
        [self.contentView addSubview:self.icon];
        
        CGFloat topHeight = 80 * KWidth / 414;
        CGFloat x = 20 * KWidth / 414;
        CGFloat headerWidth = 40 * KWidth / 414;
        self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, self.icon.height, KWidth, topHeight)];
        self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, x, headerWidth, headerWidth)];
        self.headerImageView.layer.cornerRadius = headerWidth / 2;
        self.headerImageView.layer.masksToBounds = YES;
        [self.topView addSubview:self.headerImageView];
        
        self.name = [[UILabel alloc]initWithFrame:CGRectMake(x + 10 * KWidth / 414 + headerWidth, x, (KWidth - 40 * KWidth / 414 - headerWidth), x)];
        self.name.textColor = [UIColor lightGrayColor];
        self.name.font = [UIFont systemFontOfSize:16 * KWidth / 414];
        [self.topView addSubview:self.name];
        
        self.time = [[UILabel alloc]initWithFrame:CGRectMake(x + 10 * KWidth / 414 + headerWidth, x + self.name.height, (KWidth - 40 * KWidth / 414 - headerWidth), x)];
        self.time.textColor = [UIColor grayColor];
        self.time.font = [UIFont systemFontOfSize:15 * KWidth / 414];
        [self.topView addSubview:self.time];
        
        [self.contentView addSubview:self.topView];
        
        self.contents = [[UILabel alloc]initWithFrame:CGRectMake(x, self.topView.height + self.icon.height, KWidth - 2 * x, 0)];
        self.contents.textColor = [UIColor blackColor];
        self.contents.numberOfLines = 0;
        self.contents.font = [UIFont systemFontOfSize:17 * KWidth / 414];
        [self.contentView addSubview:self.contents];
    }
    return self;
}

- (void)setModel:(CollectionDetailHeaderViewModel *)model
{
    _model = model;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:model.pic2] placeholderImage:[UIImage imageNamed:@"PCD"]];
    self.icon.frame = CGRectMake(0, 0, KWidth, KWidth * 338 / 900);
    self.topView.frame = CGRectMake(0, self.icon.height, KWidth, 80 * KWidth / 414);
    UserModel *userModel = [model.authorDic objectForKey:@"dic"];
//    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:userModel.avatar]];
    self.headerImageView.image = [UIImage imageNamed:@"Tang"];
    self.name.text = userModel.username;
    self.time.text = model.datestr;
    if (model.myDescription != nil)
    {
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:model.myDescription];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        style.lineSpacing = 6 * KWidth / 414;
        self.contents.numberOfLines = 0;
        [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, model.myDescription.length)];
        CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KWidth - 40 * KWidth / 414, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.contents.font, NSParagraphStyleAttributeName:style} context:nil];
        self.contents.attributedText = attStr;
        self.contents.frame = CGRectMake(20 * KWidth / 414, self.icon.height + self.topView.height, KWidth - 40 * KWidth / 414, bounds.size.height + 20 * KWidth / 414);
        }

}

+ (CGFloat)heightForFirstCell:(CollectionDetailHeaderViewModel *)model
{
    CGFloat height = [CallTool heightForNSStringWithString:model.myDescription width:(KWidth - 40 * KWidth / 414) fontSize:17 * KWidth / 414 space:6] + 20 * KWidth / 414;
    CGFloat iconHeight = KWidth * 338 / 900;
    CGFloat topHeight = 80 * KWidth / 414;
    return height + iconHeight + topHeight;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
